package org.common.util;

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.function.Consumer;

import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;

public class SD {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		debug(
				",是，".matches(".*[^，,是否]+.*")
				
				
				);
	
	}
	/**
	 * 打印说明
	 */
	public static void before(){
		System.out.println("调试(*^_^*)进行中***************");
	}
	
	/**
	 * debug
	 * @param o
	 */
	public  static void debug(Object o){
		StackTraceElement[] trace = new Exception().getStackTrace();
		before();
		System.out.println(o);
		System.out.println(Thread.currentThread().getStackTrace()[2]);
	}
	
	
	/**
	 * debug
	 * @param o
	 */
	public  static void debug(){
		StackTraceElement[] trace = new Exception().getStackTrace();
		before();
		System.out.println(trace[1]);
	}
	
	public static<K,V> void debug(Map<K,V> map){
		StackTraceElement[] trace = new Exception().getStackTrace();
		before();
		if(map==null){
			
		}else{
//			Set<K> set = map.keySet();
//			for(K k:set){
//				System.out.println(k+"\t:\t"+map.get(k));
//			}
			for(Map.Entry<K,V> et:map.entrySet()){
				System.out.println(et.getKey()+"\t:\t"+et.getValue());
			}
		}
		System.out.println(trace[1]);
	}
	public static void printView(List<Map<String,String>> col){

		if(col==null||col.isEmpty()){
			
		}else{
			
			for(Map<String,String> k:col){
				System.out.println(StringUtils.collectionToDelimitedString(k.values(), "\t"));
			}
		}
	}
	public static<E> void debug(Collection<E> col){
		StackTraceElement[] trace = new Exception().getStackTrace();
		before();
		if(col==null){
			
		}else{
			for(Object k:col){
				System.out.println(k);
			}
		}
		System.out.println(trace[1]);
	}
	
	public static<E> void debug(Enumeration<E> col){
		StackTraceElement[] trace = new Exception().getStackTrace();
		before();
		if(col==null){
			
		}else{
			while(col.hasMoreElements()){
				System.out.println(col.nextElement());
			}
		}
		System.out.println(trace[1]);
	}
	public static<E> void debug(byte[] bytes){
		StackTraceElement[] trace = new Exception().getStackTrace();
		before();
		if(bytes==null){
			
		}else{
			
			int len=bytes.length;
			String[] strs=new String[len];
			for(int i=0;i<len;i++){
				String v=Integer.toHexString(Byte.toUnsignedInt(bytes[i])).toUpperCase();
				if(v.length()<2){
					strs[i]="0"+v;
				}else{
					strs[i]=v;
				}
			}
			System.out.println(StringUtils.arrayToDelimitedString(strs, " "));
		}
		System.out.println(trace[1]);
	}
	
	public static void debug(Object[] col){
		StackTraceElement[] trace = new Exception().getStackTrace();
		before();
		if(col==null){
			
		}else{
			for(Object k:col){
				System.out.println(k);
			}
		}
		System.out.println(trace[1]);
	}

	
	public static boolean isValue(String value){
		return value!=null&&!"".equals(value);
	}

	public static void debugBean(Object obj){
		StackTraceElement[] trace = new Exception().getStackTrace();
		before();
		if(obj==null){
			System.out.println("对象不能为空");
			System.out.println(trace[1]);
			return;
		}
		PropertyDescriptor[] ps=BeanUtils.getPropertyDescriptors(obj.getClass());
		
		Class<String> type=String.class;
		for(PropertyDescriptor p:ps){
			
			if(type==p.getPropertyType()){
				System.out.println(p.getName()+"\t"+getPropertyValue(obj,p.getReadMethod(),type));
			}
		}
		System.out.println(trace[1]);
	}
	public static <E> E getPropertyValue(Object obj,Method getMethod,Class<E> ce){
		Object value=null;
		try {
			value = getMethod.invoke(obj, new Object[0]);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return ce.cast(value);
	}
	/**
	 * 暂停
	 */
	public static void pause(){
		Scanner scan=new Scanner(System.in);
		System.out.println("enter to end!!!!!!!!!!!");
		while(scan.hasNextLine()){
			String str=scan.nextLine();
			if(str!=null){
				break;
			}
		}
	}
	public static void pause(Consumer<String> consumer){
		Scanner scan=new Scanner(System.in);
		System.out.println("enter to end!!!!!!!!!!!"); 
		while(scan.hasNextLine()){
			String str=scan.nextLine();
			if("exit".equalsIgnoreCase(str)){
				break;
			}
			consumer.accept(str);
		}
	}

}
